package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"mxshop_srvs/inventory_srv/proto"
	"sync"
)

var client proto.InventoryClient

func Init() {
	conn, _ := grpc.Dial(":50053", grpc.WithTransportCredentials(insecure.NewCredentials()))

	client = proto.NewInventoryClient(conn)
}

func main() {
	Init()
	var wg sync.WaitGroup
	wg.Add(20)
	for i := 0; i < 20; i++ {
		InvSell(&wg)
	}
	wg.Wait()
}


func InvSell(wg *sync.WaitGroup) {
	defer wg.Done()
	_, err := client.Sell(context.Background(), &proto.SellInfo{
		GoodsInfo: []*proto.GoodsInvInfo{
			{
				GoodsId: 1,
				Num:     1,
			},
		},
	})
	if err != nil {
		panic(err)
	}
	fmt.Println("库存扣减成功")
}